﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace Nirvana
{
    [CustomEditor(typeof(BezierSpline))]
    public sealed class BezierSplineEditor : Editor
    {
        private static readonly Color[] colors = new Color[]
        {
            Color.white,
            Color.yellow,
            Color.cyan
        };

        private SerializedProperty loop;
        private int PointCount = -1;

        private void OnEnable()
        {
            this.loop = serializedObject.FindProperty("loop");
        }

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.loop, new GUILayoutOption[0]);
            BezierSpline bezierSpline = this.target as BezierSpline;
            if (this.PointCount >= 0 && this.PointCount < bezierSpline.PointCount)
            {
                this.SetPoint(bezierSpline);
            }
            if (GUILayout.Button("Add Curve", new GUILayoutOption[0]))
            {
                Undo.RecordObject(bezierSpline, "Add Curve");
                bezierSpline.AddCurve();
                EditorUtility.SetDirty(bezierSpline);
            }
            base.serializedObject.ApplyModifiedProperties();
        }


        private void SetPoint(BezierSpline bzSpline)
        {
            GUILayout.Label("Selected Point", new GUILayoutOption[0]);
            EditorGUI.BeginChangeCheck();
            Vector3 vector = bzSpline.GetPoint(this.PointCount);
            vector = EditorGUILayout.Vector3Field("Position", vector, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                bzSpline.SetPoint(this.PointCount, vector);
            }
            EditorGUI.BeginChangeCheck();
            BezierSpline.PointMode mode = (BezierSpline.PointMode)EditorGUILayout.EnumPopup("Mode", bzSpline.GetPointMode(this.PointCount), new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                bzSpline.SetPointMode(this.PointCount, mode);
            }
        }

        private void OnSceneGUI()
        {
            BezierSpline bezierSpline = this.target as BezierSpline;
            Quaternion quaternion = (Tools.pivotRotation != null) ? Quaternion.identity : bezierSpline.transform.rotation;
            Vector3 vector = this.MovePoint(bezierSpline, quaternion, 0);
            for (int i = 1; i < bezierSpline.PointCount; i += 3)
            {
                Vector3 vector2 = this.MovePoint(bezierSpline, quaternion, i);
                Vector3 vector3 = this.MovePoint(bezierSpline, quaternion, i + 1);
                Vector3 vector4 = this.MovePoint(bezierSpline, quaternion, i + 2);
                Handles.color = Color.gray;
                Handles.DrawLine(vector, vector2);
                Handles.DrawLine(vector3, vector4);
                Handles.DrawBezier(vector, vector4, vector2, vector3, Color.white, null, 2f);
                vector = vector4;
            }
            this.DrawLine(bezierSpline);
        }
        private void DrawLine(BezierSpline bz)
        {
            Handles.color = Color.green;
            Vector3 point = bz.GetPosition(0f);
            Handles.DrawLine(point, point + bz.GetDirection(0f) * 0.5f);
            int num = 10 * bz.CurveCount;
            for (int i = 1; i <= num; i++)
            {
                float t = (float)i / (float)num;
                point = bz.GetPosition(t);
                Handles.DrawLine(point, point + bz.GetDirection(t) * 0.5f);
            }
        }
        private Vector3 MovePoint(BezierSpline bz, Quaternion A_2, int count)
        {
            Vector3 vector = bz.transform.TransformPoint(bz.GetPoint(count));
            float num = HandleUtility.GetHandleSize(vector);
            if (count == 0)
            {
                num *= 2f;
            }
            Handles.color = colors[(int)bz.GetPointMode(count)];
            if (Handles.Button(vector, A_2, num * 0.04f, num * 0.06f, Handles.DotHandleCap))
            {
                this.PointCount = count;
                this.Repaint();
            }
            if (this.PointCount == count)
            {
                EditorGUI.BeginChangeCheck();
                vector = Handles.DoPositionHandle(vector, A_2);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(bz, "Move Point");
                    EditorUtility.SetDirty(bz);
                    bz.SetPoint(count, bz.transform.InverseTransformPoint(vector));
                }
            }
            return vector;
        }

    }
}

